/*
 * AddCarFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.ruif.view;

import javax.swing.JOptionPane;

import com.ruif.client.CarServiceImpl;
import com.ruif.model.Car;
import com.ruif.service.ICarService;
import com.ruif.util.DateUtil;
import com.ruif.util.StringUtil;

/**
 *
 * @author  __USER__
 */
public class AddCarFrame extends javax.swing.JInternalFrame
{
    private static final long serialVersionUID = -6888860412337209667L;

    ICarService carService = new CarServiceImpl();

    /** Creates new form AddCarFrame */
    public AddCarFrame()
    {
        initComponents();
        this.setLocation(200, 50);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents()
    {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jTextField_id = new javax.swing.JTextField();
        jTextField_licenseNo = new javax.swing.JTextField();
        jTextField_price = new javax.swing.JTextField();
        jTextField_type = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea_desc = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);

        jLabel1.setText("\u7f16 \u53f7\uff1a");

        jLabel2.setText("\u8f66\u724c\u53f7\uff1a");

        jLabel3.setText("\u4ef7 \u683c\uff1a");

        jLabel4.setText("\u7c7b \u578b\uff1a");

        jLabel6.setText("\u63cf \u8ff0\uff1a");

        jTextField_id.setEditable(false);

        jTextArea_desc.setColumns(20);
        jTextArea_desc.setRows(5);
        jScrollPane1.setViewportView(jTextArea_desc);

        jButton1.setIcon(new javax.swing.ImageIcon("F:\\WorkSpaces\\reif_swing_utf8\\images\\add.png")); // NOI18N
        jButton1.setText("\u63d0\u4ea4");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon("F:\\WorkSpaces\\reif_swing_utf8\\images\\reset.png")); // NOI18N
        jButton2.setText("\u91cd\u7f6e");
        jButton2.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                                false)
                                                                                .addGroup(
                                                                                        layout.createSequentialGroup()
                                                                                                .addComponent(jLabel1)
                                                                                                .addPreferredGap(
                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                .addComponent(
                                                                                                        jTextField_id))
                                                                                .addGroup(
                                                                                        layout.createSequentialGroup()
                                                                                                .addComponent(jLabel3)
                                                                                                .addPreferredGap(
                                                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                .addComponent(
                                                                                                        jTextField_price,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                        103,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                                .addGap(23, 23, 23)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                .addComponent(jLabel4)
                                                                                .addComponent(jLabel2))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                                false)
                                                                                .addComponent(jTextField_type)
                                                                                .addComponent(
                                                                                        jTextField_licenseNo,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        110, Short.MAX_VALUE)))
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(jLabel6)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(
                                                                                        jScrollPane1,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        288, Short.MAX_VALUE)
                                                                                .addGroup(
                                                                                        layout.createSequentialGroup()
                                                                                                .addComponent(
                                                                                                        jButton1,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                        88,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                                .addGap(44, 44, 44)
                                                                                                .addComponent(
                                                                                                        jButton2,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                        86,
                                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addContainerGap(40, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(
                                                        layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(jLabel1)
                                                                .addComponent(jTextField_licenseNo,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(
                                                        layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(jTextField_id,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(jLabel2)))
                                .addGap(26, 26, 26)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jLabel3)
                                                .addGroup(
                                                        layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                .addComponent(jTextField_type,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(jTextField_price,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(jLabel4)))
                                .addGap(33, 33, 33)
                                .addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addComponent(jScrollPane1,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(18, 18, 18)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                .addComponent(
                                                                                        jButton1,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        34,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                .addComponent(
                                                                                        jButton2,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        34,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                                                .addComponent(jLabel6)).addGap(52, 52, 52)));

        pack();
    }// </editor-fold>
    //GEN-END:initComponents

    /**
     * 触发新增
     * @param evt
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        String licenseNo = this.jTextField_licenseNo.getText().trim();
        String price = this.jTextField_price.getText().trim();
        String type = this.jTextField_type.getText().trim();
        String date = DateUtil.getCurrentDate("yyyy/MM/dd HH:mm:ss");
        String desc = this.jTextArea_desc.getText().trim();

        /**
         * 判断提交参数
         */
        if (StringUtil.isEmpty(licenseNo))
        {
            JOptionPane.showMessageDialog(null, "车牌号不能为空！");
            return;
        }
        if (desc.length() < 5)
        {
            JOptionPane.showMessageDialog(null, "请用5或>5个字描述");
            return;
        }

        Car car = new Car(licenseNo, Double.parseDouble(price), type, date, desc);
        
        int ok = carService.addCar(car);
        if (ok == 0)
        {
            JOptionPane.showMessageDialog(null, "操作失败！");
            return;
        }

        JOptionPane.showMessageDialog(null, "新增成功！");
        this.resetValue();

    }

    /**
     * 触发重置事件
     * @param evt
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
    {
        this.resetValue();
    }

    private void resetValue()
    {
        this.jTextField_id.setText("");
        this.jTextField_licenseNo.setText("");
        this.jTextField_price.setText("");
        this.jTextField_type.setText("");
        this.jTextArea_desc.setText("");
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea_desc;
    private javax.swing.JTextField jTextField_id;
    private javax.swing.JTextField jTextField_licenseNo;
    private javax.swing.JTextField jTextField_price;
    private javax.swing.JTextField jTextField_type;
    // End of variables declaration//GEN-END:variables

}